//
//  SNMapViewController.m
//  project
//
//  Created by 圆圆陈 on 2017/12/11.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNMapViewController.h"
#import "CountDownView.h"
#import "SNMapView.h"
#import <CoreLocation/CoreLocation.h>
#import "SNTrackSetViewController.h"
#import "SNShareViewController.h"
@interface SNMapViewController ()<UIAlertViewDelegate>
@property(nonatomic,strong)CountDownView *countDownView;
@property(nonatomic,strong)SNMapView *mapView;
@end

@implementation SNMapViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _mapView.weatherView.hidden = !SN_USER.boolShoWeatherInfo;
    _mapView.temperatureLabel.hidden = !SN_USER.boolShoWeatherInfo;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _mapView = [[SNMapView alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20), HHHWIDTH, HHHHEIGHT-20-FitSimpleIpx(0, 20))];
    [self.view addSubview:_mapView];
    __weak SNMapViewController *weakSelf = self;
    _mapView.BackBtnClickBlock = ^(UIButton *backBtn) {
        [weakSelf releaseMapview];
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    
    _mapView.SetBtnClickBlock = ^(UIButton *setBtn) {
        SNTrackSetViewController *setVC = [[SNTrackSetViewController alloc]init];
        [weakSelf.navigationController pushViewController:setVC animated:YES];
    };
    
    _mapView.ShareBtnClickBlock = ^(UIButton *shareBtn) {
        UIGraphicsBeginImageContext(CGSizeMake(screenWidth, screenHeight));
        
        [[UIApplication sharedApplication].delegate.window.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        NSMutableArray *items = [[NSMutableArray alloc] init];
        [items addObject:image];
        
        UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
        NSMutableArray           *excludedActivityTypes  = [NSMutableArray arrayWithArray:@[
                                                                                            UIActivityTypeMessage, UIActivityTypeMail, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo, UIActivityTypeAirDrop,
                                                                                            UIActivityTypeOpenInIBooks]];
        activityViewController.excludedActivityTypes = excludedActivityTypes;
        
        [weakSelf presentViewController:activityViewController animated:YES completion:nil];
        activityViewController.completionWithItemsHandler = ^(UIActivityType __nullable activityType, BOOL completed, NSArray *__nullable returnedItems, NSError *__nullable activityError){
        };
    };
    _mapView.StartBtnClickBlock = ^(UIButton *startBtn) {
        _countDownView = [[CountDownView alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20), HHHWIDTH, HHHHEIGHT-20-FitSimpleIpx(0, 20))];
        [weakSelf.view addSubview:weakSelf.countDownView];
    };
    [self locationServicesEnabled];
   
}
-(void)locationServicesEnabled {
    if (![CLLocationManager locationServicesEnabled]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:HHH_Text(@"手机GPS功能未打开，会导致当前位置与实际位置有较大差异，请开启手机GPS功能")  delegate:self cancelButtonTitle:nil otherButtonTitles:HHH_Text(@"现在去开启"),HHH_Text(@"退出我的跑道"),nil];
        [[UIView appearance] setTintColor:UIColorRGB(72, 72, 72)];
        alert.tag=100;
        [alert show];
    }else{
        BOOL show = YES;
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"alertBool"]) {
            show = [[[NSUserDefaults standardUserDefaults] objectForKey:@"alertBool"] isEqualToString:@"NO"]?NO:YES;
        }
        if (show) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:HHH_Text(@"室内运动会导致运动轨迹记录不准，请于户外使用") delegate:self cancelButtonTitle:nil otherButtonTitles:HHH_Text(@"确定"),HHH_Text(@"不再提醒"), nil];
            [[UIView appearance] setTintColor:UIColorRGB(72, 72, 72)];
            alert.tag=101;
            [alert show];
        }
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(alertView.tag==100){
        if (buttonIndex==0) {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            BOOL show = YES;
            if ([[NSUserDefaults standardUserDefaults] objectForKey:@"alertBool"]) {
                show = [[[NSUserDefaults standardUserDefaults] objectForKey:@"alertBool"] isEqualToString:@"NO"]?NO:YES;
            }
            if (show) {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:HHH_Text(@"室内运动会导致运动轨迹记录不准，请于户外使用") delegate:self cancelButtonTitle:nil otherButtonTitles:HHH_Text(@"确定"),HHH_Text(@"不再提醒"), nil];
                [[UIView appearance] setTintColor:UIColorRGB(72, 72, 72)];
                alert.tag=101;
                [alert show];
            }
        }else{
            [self releaseMapview];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else{
        if (buttonIndex==1) {
            [[NSUserDefaults standardUserDefaults] setObject:@"NO"  forKey:@"alertBool"];
            [[NSUserDefaults standardUserDefaults]synchronize];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
-(void)releaseMapview{
    [SingletonMapView sharedInstance].showsUserLocation = NO;
    [SingletonMapView sharedInstance].delegate = nil;
    [[SingletonMapView sharedInstance] removeAnnotations:[SingletonMapView sharedInstance].annotations];
    [[SingletonMapView sharedInstance] removeOverlays:[SingletonMapView sharedInstance].overlays];
    [[SingletonMapView sharedInstance] removeFromSuperview];
}
-(void)viewWillDisappear:(BOOL)animated{
   [super viewWillDisappear:animated];
}


@end
